package com.bsth.controller;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoy on 2017/5/11.
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @PostMapping(value = "/excel")
    public Map excel(MultipartFile file){
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(file.getOriginalFilename());
        try {
            Workbook wk = null;
            Sheet sheet = null;
            //判断是2007以后的，还是2003以前的
            if(file.getOriginalFilename().toLowerCase().indexOf(".xlsx") != -1){
                wk = new XSSFWorkbook(file.getInputStream());
                sheet = wk.getSheetAt(0);
            }else {
                POIFSFileSystem fs = new POIFSFileSystem(file.getInputStream());
                wk = new HSSFWorkbook(fs);
                sheet = wk.getSheetAt(0);
            }
            //总行数
            int row = sheet.getLastRowNum();
            //System.out.println(row);
            //总列数
            int colNum = sheet.getRow(0).getPhysicalNumberOfCells();
            boolean flag = false;
            Cell cell = null;
            for (int i = 3 ; i < row ; i++ ){
                for (int j = 0 ; j < colNum ; j++ ){
                    //获取单元格
                    cell = sheet.getRow(i).getCell(j);
                    String content = "##";
                    System.out.print(cell.getCellType()+"**");
                    if(cell.getCellType() == Cell.CELL_TYPE_STRING){
                        content = cell.getStringCellValue()+"--";
                    } else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
                        //判断是不是日期
                        if(DateUtil.isCellDateFormatted(cell) || cell.getCellStyle().getDataFormat() == 58){
                            Date date = cell.getDateCellValue();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            content = sdf.format(date).toString()+"--";
                        }else {
                            content = (int) cell.getNumericCellValue()+"--";
                        }
                    }
                    if(content.indexOf("备注") != -1){
                        flag = true;
                        break;
                    }
                    System.out.print(content);
                }
                if (flag){
                    break;
                }
                System.out.println();
            }
            map.put("success","success");
        } catch (IOException e) {
            e.printStackTrace();
            map.put("error","error");
        }
        return map;
    }
}
